using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using THSERP.BusinessEntities;using THSERP.BaseProvider;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using THSLib;using THSERP.BaseProvider;
using THSERP.BaseProvider;
namespace THSERP
{
    public partial class guiRefundDetails : THSERPScreen
    {
        private double TotalRefund;
        private double InitialAmount;
        public THSList<ARDocumentPaymentDetailsInfo> PaymentList = new THSList<ARDocumentPaymentDetailsInfo>();

        public guiRefundDetails()
        {
            InitializeComponent();
        }

        public guiRefundDetails(double totalRefund, double initialAmount)
        {
            InitializeComponent();

            this.TotalRefund = totalRefund;
            this.InitialAmount = initialAmount;

            //Get receipt total
            
            fld_lblTotalRefund.Text = THSUtil.GetNumberDisplayFormat(TotalRefund);

            //Init payment method grid
            PaymentList.Clear();
            foreach (DataRow configRow in ADConfigValueUtility.glbdsConfigValues.Tables[ADConfigValueUtility.ADConfigValuePaymentMethod].Rows)
            {
                ARDocumentPaymentDetailsInfo objDocumentPaymentDetailsInfo = new ARDocumentPaymentDetailsInfo();
                objDocumentPaymentDetailsInfo.ARPaymentMethodCombo = configRow["Value"].ToString();
                objDocumentPaymentDetailsInfo.ARPaymentMethodDisplayText = configRow["Text"].ToString();
                if (objDocumentPaymentDetailsInfo.ARPaymentMethodCombo == ADConfigValueUtility.cstPaymentMethodCash ||
                    objDocumentPaymentDetailsInfo.ARPaymentMethodCombo == ADConfigValueUtility.cstPaymentMethodCreditNote ||
                    objDocumentPaymentDetailsInfo.ARPaymentMethodCombo == ADConfigValueUtility.cstPaymentMethodGiftVoucher)
                    PaymentList.Add(objDocumentPaymentDetailsInfo);
            }
            fld_dgcPayment.DataSource = PaymentList;
            ProposeRemainingAmount();

            GridView gridView = (GridView)fld_dgcPayment.MainView;
            gridView.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(GridView_FocusedRowChanged);
        }

        private void GridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            ProposeRemainingAmount();
        }

        public void ProposeRemainingAmount()
        {
            ARDocumentPaymentDetailsInfo currentPayment = GetCurrentRowData();
            if (currentPayment != null)
            {
                double amount = 0;
                foreach (ARDocumentPaymentDetailsInfo objDocumentPaymentDetaisInfo in PaymentList)
                    if (objDocumentPaymentDetaisInfo.ARPaymentMethodCombo != currentPayment.ARPaymentMethodCombo)
                        amount += objDocumentPaymentDetaisInfo.ARDocumentPaymentDetailAmount;

                if (InitialAmount > amount)
                    currentPayment.ARDocumentPaymentDetailAmount = InitialAmount - amount;
                fld_dgcPayment.RefreshDataSource();
            }
        }

        public ARDocumentPaymentDetailsInfo GetCurrentRowData()
        {
            GridView gridView = (GridView)fld_dgcPayment.MainView;
            int index = gridView.GetDataSourceRowIndex(gridView.FocusedRowHandle);
            if (index >= 0)
                return PaymentList[index];
            return null;
        }

        private void fld_btnAcceptPayment_Click(object sender, EventArgs e)
        {
            if (fld_btnAcceptPayment.Tag.ToString() == "Accept")
            {
                
                ERPModuleEntities entity = ((BaseModuleERP)Module).CurrentModuleEntity;
                double amount = 0;
                foreach (ARDocumentPaymentDetailsInfo objDocumentPaymentDetailsInfo in PaymentList)
                    amount += objDocumentPaymentDetailsInfo.ARDocumentPaymentDetailAmount;
                if (amount < TotalRefund)
                {
                    MessageBox.Show("The refund amount is less than the total refund. Please define refund amount again.", "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                fld_lblReceivingAmount.Text = THSUtil.GetNumberDisplayFormat(amount - TotalRefund);
                fld_lblChangeAmount.Text = THSUtil.GetNumberDisplayFormat(amount);

                //Invalidate controls
                fld_btnAcceptPayment.Text = "Discard Payment";
                fld_btnAcceptPayment.Tag = "Discard";
                fld_btnComplete.Enabled = true;
                fld_dgcPayment.Enabled = false;
            }
            else if (fld_btnAcceptPayment.Tag.ToString() == "Discard")
            {
                fld_btnAcceptPayment.Text = "Accept Payment";
                fld_btnAcceptPayment.Tag = "Accept";
                fld_btnComplete.Enabled = false;
                fld_dgcPayment.Enabled = true;
            }
        }

        private void fld_btnComplete_Click(object sender, EventArgs e)
        {
            this.Tag = "Complete";
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void fld_btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}